using System;
using System.Collections.Generic;
using System.Linq;
using ApiProject.Api.Data;
using ApiProject.Api.Entity;
using ApiProject.Api.PramaModel;
using ApiProject.Api.Repository;
using ApiProject.Api.Uitls;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;

//商品分类
namespace ApiProject.Api.Controllers
{
    // [Authorize]
    [ApiController]
    [Route("[controller]")]
    public class CommskuController : ControllerBase
    {
        private IRepository<CommodityDetails> _commskuRepository;
        private IRepository<Commodities> _commdityRepository;

        public CommskuController(
            IRepository<CommodityDetails> commskuRepository,
            IRepository<Commodities> commdityRepository
        )
        {
            _commskuRepository = commskuRepository;
            _commdityRepository=commdityRepository;

        }

    
        // 获取全部Sku
        // Get /commoditytype
        [HttpGet("{id}")]
        public dynamic GetAllSku(int id)
        {
            // var skubyuserId=_commskuRepository.Table.Where(x=>x.UserId==id && x.IsDelete==false).ToList();

            var spu = from commsku in _commskuRepository.Table
                      join commspu in _commdityRepository.Table on commsku.CommodityId equals commspu.Id
                      where commsku.UserId == id && commsku.IsDelete == false
                      select new { Id = commsku.Id, CommodityId=commspu.Id,CommodityName=commspu.CommodityName,
                        CommodityExplain=commspu.CommodityExplain,Price=commsku.Price,Inventory=commsku.Inventory,
                        CreateTime=commsku.CreateTime,IsEnable=commsku.IsGrounding
                      };


            

            return JsonHelper.Serialize(new
            {
                Code = 200,
                Data = spu,
                Msg = "查询成功"
            });
        }

        //删除商品Sku
        [HttpPost,Route("deletesku/{id}")]
        public dynamic deletesku(int id)
        {
            var commsku=_commskuRepository.GetById(id);
            commsku.IsDelete=true;
            commsku.IsEnable=false;
            commsku.UpdateTime=DateTime.Now;
            _commskuRepository.Update(commsku);
            return JsonHelper.Serialize(new
            {
                Code = 200,
                Data = commsku,
                Msg = "删除成功！"
            });

        }

        //修改上架状态
        [HttpPut]
        public dynamic IsGrsku(NewGrounding model)
        {
            var skuId=model.Id;
            var isGrounding=model.IsEnable;

            var commsku=_commskuRepository.GetById(skuId);
            commsku.IsGrounding=isGrounding;
            commsku.UpdateTime=DateTime.Now;
            _commskuRepository.Update(commsku);
            return JsonHelper.Serialize(new
            {
                Code = 200,
                Data = commsku,
                Msg = "修改成功！"
            });

        }






        
    }
}
